package com.weiyi.mashangzhu.Roomer.Adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.HouseDetailsActivity;
import com.weiyi.mashangzhu.Roomer.Activity.MessageDetailsActivity;
import com.weiyi.mashangzhu.Roomer.Bean.AllCommentInfo;
import com.weiyi.mashangzhu.Roomer.Bean.CollectInfoBean;
import com.weiyi.mashangzhu.Roomer.Bean.ThemeBean;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import static com.baidu.location.h.k.v;

/**
 * Created by win7 on 2017/5/12.
 */

public class CollectAdapter extends BaseAdapter {
    List<CollectInfoBean.DataBean> list;
    Context context;

    public CollectAdapter(List<CollectInfoBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }
    private void showNormalDialog(final int p) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        normalDialog.setIcon(R.mipmap.tishi);
        normalDialog.setTitle("提示");
        normalDialog.setMessage("你确定要取消收藏此房源吗?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LoadingDialog.show(context);
                        OkHttpUtils.get().addParams("token", SPUtils.get(context, "token", "") + "")
                                .addParams("id",list.get(p).getHouseId()+"")
                                .url(API.BaseApi_R+API.CollectHouse_Api)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        LoadingDialog.dismiss(context);
                                        T.showShort(context, "取消收藏失败");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        Log.e("tyyyyyyy","re="+response);
                                        LoadingDialog.dismiss(context);
                                        try {
                                            JSONObject jsonObject=new JSONObject(response);
                                            if (jsonObject.getInt("status")==0){
                                                T.showShort(context, "取消收藏成功");
                                                list.remove(p);
                                                notifyDataSetChanged();
                                            }
                                            T.showShort(context,jsonObject.getString("msg"));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_collect_list, null);
            viewHolder.grade = (TextView) convertView.findViewById(R.id.collect_grade);
            viewHolder.pricenum = (TextView) convertView.findViewById(R.id.price_num);
            viewHolder.title = (TextView) convertView.findViewById(R.id.collect_title);
            viewHolder.type = (TextView) convertView.findViewById(R.id.collect_type);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.collect_img);
            viewHolder.mark = (ImageView) convertView.findViewById(R.id.collect_mark);

            convertView.setTag(viewHolder);

        }
        viewHolder = (ViewHolder) convertView.getTag();

        viewHolder.grade.setText(list.get(position).getHouse().getScoreave()+"");
        viewHolder.pricenum.setText(list.get(position).getHouse().getHPrice()+"");
        viewHolder.title.setText(list.get(position).getHouse().getHTitle());
        viewHolder.type.setText(list.get(position).getHouse().getHRoom()+"居  |  "+list.get(position).getHouse().getHDistrict());
        if (MyApp.getUrlList4(list,position).size()!=0){
                    GlideImgManager.glideLoader(context, API.BaseApi_url+ MyApp.getUrlList4(list,position).get(0),R.mipmap.errer,viewHolder.img);
        }else {
            viewHolder.img.setImageResource(R.mipmap.errer);
        }

        viewHolder.mark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog(position);

            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, HouseDetailsActivity.class);
                intent.putExtra("city",list.get(position).getHouse().getHCity());
                intent.putExtra("id",list.get(position).getHouseId()+"");
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView grade,pricenum,title,type;
        ImageView img,mark;
    }
}
